package com.baoleme.service.impl;

import com.baoleme.entity.ShoppingCart;
import com.baoleme.exceptions.BusinessException;
import com.baoleme.exceptions.enumeration.ResponseEnum;
import com.baoleme.mapper.ShoppingCartMapper;
import com.baoleme.service.ShoppingCartService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuyunbin
 * @since 2024-09-25
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Override
    public List<ShoppingCart> listByUserId(String userId) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getMemberId, userId);
        if (this.count(wrapper) > 0) {
            return this.list(wrapper);
        } else {
            throw new BusinessException(ResponseEnum.EMPTY_SHOPPING_CART);
        }
    }

    @Override
    public boolean removeByUserId(String userId) {
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getMemberId, userId);
        if (this.count(wrapper)>0){
            return this.remove(wrapper);
        }else {
            throw new BusinessException(ResponseEnum.EMPTY_SHOPPING_CART);
        }
    }
}
